import Vue from 'vue'
import Vuex from 'vuex'
//导入日志
import { createLogger } from 'vuex'
//导入持久化插件
import createPersistedState from "vuex-persistedstate";

Vue.use(Vuex)

import { shop_favlist, shop_fav, shop_unfav } from '../utils/api'
export default new Vuex.Store({
    state: {
        collectList: [],

    },
    getters: {

    },
    mutations: {
        collect(state, payload) {
            state.collectList.push(payload);
            // console.log(payload)
            // console.log(state.collectList);
        },
        cancel_collect(state, payload) {
            var index = state.collectList.findIndex((item) => {
                return item.mtWmPoiId == payload.mtWmPoiId;
            })
            if (index != -1) {
                state.collectList.splice(index, 1);
            }
            // console.log(state.collectList);
        },
        save_favlist(state, payload) {
            state.collectList = payload;
            console.log(state.collectList);
        }

    },
    actions: {
        request_favlist(store) {
            var userid = JSON.parse(localStorage.getItem('userinfo')).id;
            // console.log(userid);
            shop_favlist({ userid }).then((res) => {
                store.commit('save_favlist', res.data.list);
                console.log(res.data.list);
            })
        }
    },
    modules: {

    },
    plugins: [createLogger(), createPersistedState()]
})